package com.mc.moring.widge

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.core.view.updateLayoutParams
import com.mc.moring.R

/**
 * Author : hwm
 * Date : 2023/12/27 16:19
 * Description : 主页引导对话框
 */
class HomeGuideDialog(context: Context, private var targetView: View) :
    Dialog(context, R.style.FullScreenTransparentDialog) {
    private val cl by lazy { targetView!!.findViewById<ConstraintLayout>(R.id.cl) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_home_guide)
//        setDialogFullScreen()
        setBackgroundTransparent(0f)
        val tvHomeGuideContent = findViewById<TextView>(R.id.tv_home_guide_content)
        tvHomeGuideContent.updateLayoutParams<ViewGroup.MarginLayoutParams> {
            val array = IntArray(2)
            targetView.getLocationOnScreen(array)
            marginStart = array[0]
            topMargin = array[1]
        }
        cl.setOnClickListener { dismiss() }
    }

    private fun setDialogFullScreen() {
        if (window != null) {
            window!!.setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            window!!.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window!!.getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            )
            window!!.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window!!.setStatusBarColor(Color.TRANSPARENT)
            ViewCompat.getWindowInsetsController(window!!.getDecorView())
                ?.setAppearanceLightStatusBars(true)
        }
    }

    /**
     * 设置背景透明度
     * @param fraction 0-1之间，0是透明1是全黑
     */
    public fun setBackgroundTransparent(fraction: Float) {
        window?.setDimAmount(fraction)
    }
}